import { HistoryLinkState, LinkListProps } from '@/interfaces/vuex/historyLink'
import { StoreOptions } from 'vuex'
import router from '@/router'

const menuModel: StoreOptions<HistoryLinkState> = {
  state: {
    linkList: [],
  },
  mutations: {
    setLinkList(state, value: LinkListProps) {
      let num = 0
      state.linkList.forEach((item) => {
        if (item.key === value.key) {
          item.type = 'success'
        } else {
          item.type = ''
          num += 1
        }
      })
      if (num === state.linkList.length) {
        state.linkList.push(value)
      }
    },
    deleteLinkListByKey(
      state,
      oldBreadcrumbList: {
        key: string
        value: string
        type: 'info' | 'success'
      }
    ) {
      if (state.linkList.length === 1) return
      let index = 0
      state.linkList.forEach((item, i) => {
        if (item.key === oldBreadcrumbList.key) {
          index = i
        }
      })
      if (state.linkList[index].type === 'success') {
        let key: string | number = ''
        if (state.linkList[index - 1] && state.linkList[index - 1].key) {
          key = state.linkList[index - 1].key
        } else {
          key = state.linkList[index + 1].key
        }
        router.push({
          name: key as string,
        })
      }
      state.linkList.splice(index, 1)
    },
    setLinkZreo(state) {
      state.linkList = []
    }
  },
  actions: {},
}

export default menuModel
// MenuColumnState
